angular.module('app.services')
.service( 'UserAuth', ['$log', '$localStorage', 'AppConfig',function( $log, $localStorage, AppConfig ){
	var localVersion = $localStorage.version || '';
	if( localVersion !== AppConfig.version ){
		$localStorage.user_auth = { token: '', username: ''};
		$localStorage.version = AppConfig.version;
	}
	if( !$localStorage.user_auth ){
		$localStorage.user_auth = { token: '', username: ''};
	}
	var getUserAuth = function(){
		var _userAuth = $localStorage.user_auth ;
		return _userAuth;
	}
	getUserAuth();
	return {
		saveUserAuth: function( user ){
			if( typeof user != 'object' ) return;
			var userAuth = getUserAuth();
			for(var pro in userAuth){
    	    	if( user.hasOwnProperty(pro) ){
    	    		userAuth[pro] = user[pro];
    	    	}
    	    }
			$localStorage.user_auth =  userAuth;
			return userAuth;
		},
		refreshToken: function( token ){
	        var _userAuth = getUserAuth();
	        _userAuth.token = token;
	        $localStorage.user_auth = _userAuth;
		},
		getUserAuth: function(){
			return getUserAuth();
		}
	}
}]);
